<?php

namespace App\Services\MakeMoment;


use App\Models\MomentComment;
use App\Models\MomentReply;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class ReplyMomentRequest extends FormRequest
{
	protected ?Moment $moment = null;

	public function rules(): array
	{
		
		$momentCommentId =  (int)request('moment_comment_id');
		if ($momentCommentId) {
			$this->momentComment = MomentComment::find($momentCommentId);
		}

		$momentReplyId =  (int)request('parent_moment_reply_id');
		if ($momentReplyId) {
			$this->momentReply = MomentReply::find($momentReplyId);
		}else{
			$this->momentReply = null;
		}

		return [
			'moment_comment_id' => ['required', $this->momentComment()],
			'parent_moment_reply_id' => ['integer',$this->momentReply()],
			'content' => 'required|nullable|string',
		];
	}

	public function momentComment()
	{
		return function ($attribute, $value, $fail) {
			if (!$this->momentComment) {
				return $fail('该评论不存在');
			} 
			return true;
		};
	}

	public function momentReply()
	{
		return function ($attribute, $value, $fail) {
			if($value){
				if(!$this->momentReply){
					return $fail('该回复不存在');
				}
			}
			return true;
		};
	}



	public function getMomentComment(): MomentComment
	{
		return $this->momentComment;
	}


	public function getMomentReply()
	{
		return $this->momentReply;
	}
}
